﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Mvc;
using Redboil.Bss.Service;
using System.Web.Security;
using Redboil.Bss.Core;

namespace Redboil.Bfp.Web.Controllers {
    public class AuthenticationController : Controller {

        private IAuthenticationService authenticationService;
        private Boolean isValidated;

        public AuthenticationController() {
            authenticationService = Container.Instance.Resolve<IAuthenticationService>();
        }

        public ActionResult SignIn() {
            return View("SignInView");
        }

        public void Validate() {
            String userName = Request.Form["TextBoxUserName"];
            if (userName.Trim().Length == 0) throw new Redboil.Bss.ApplicationException("用户名或密码错误");
            String password = Request.Form["TextBoxPassword"];
            if (password.Trim().Length == 0) throw new Redboil.Bss.ApplicationException("用户名或密码错误");
            isValidated = authenticationService.Validate(userName, password);

            //ViewData["Categories"] = authenticationService.Validate();

            //FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
            //    1,                                 // version
            //    txtUserName.Text,                  // user name
            //    DateTime.Now,                      // creation
            //    DateTime.Now.AddMinutes(60),       // Expiration
            //    false,                             // Persistent
            //    roles
            //);

            FormsAuthentication.SetAuthCookie(userName, false);
            if (String.IsNullOrEmpty(Request["ReturnUrl"])) Response.Redirect(FormsAuthentication.DefaultUrl);
            FormsAuthentication.RedirectFromLoginPage(userName, false);
        }

        public ActionResult Register() {
            return View("RegisterView");
        }

        public ActionResult SignOut() {
            FormsAuthentication.SignOut();
            return View("SignInView");
        }
    }
}
